package com.gz.spring.factory;

import com.gz.spring.bean.Monster;
import org.springframework.beans.factory.FactoryBean;

import java.util.HashMap;
import java.util.Map;

public class MyFactoryBean implements FactoryBean<Monster> {

    //这里的key就是指定要获取的对象，如 monster01
    private String key;
    private Map<String,Monster> monster_map;

    {
        monster_map = new HashMap<>();
        monster_map.put("monster01",new Monster(100,"牛魔王","芭蕉扇"));
        monster_map.put("monster02",new Monster(200,"狐狸精","美人计"));
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }


    @Override
    public Monster getObject() throws Exception {
        return monster_map.get(key);
    }

    @Override
    public Class<?> getObjectType() {
        return Monster.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
